package com.xiaobai.config;

import com.xiaobai.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangdaji
 * @version 1.0
 * @Description: 授权服务配置
 * @date 22-2-19 上午 1:50
 */
@Component
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    @Qualifier("redisTokenStore")
    private TokenStore redisTokenStore;

    @Autowired
    @Qualifier("jwtTokenStore")
    private TokenStore jwtTokenStore;

    /**
     * oauth2&token转换器
     */
    @Autowired
    @Qualifier("getTokenAccess")
    private JwtAccessTokenConverter jwtAccessTokenConverter;

    @Autowired
    @Qualifier("getTokenEnhancer")
    private JwtTokenEnhancer jwtTokenEnhancer;

    /**
     * 使用密码模式的配置
     *
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        //配置jwttoken增强器
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        List<TokenEnhancer> list = new ArrayList<>();
        list.add(jwtTokenEnhancer);
        list.add(jwtAccessTokenConverter);
        tokenEnhancerChain.setTokenEnhancers(list);

        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userService)
                //使用redis存储token
                // .tokenStore(redisTokenStore);
                //使用令牌存储策略
                .tokenStore(jwtTokenStore)
                //配置oauth2和token转换器
                .accessTokenConverter(jwtAccessTokenConverter)
                .tokenEnhancer(tokenEnhancerChain);
    }


    /**
     * 通过client_id和密码,设置令牌的有效时间
     *
     * @param clients
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //存入内存中
        clients.inMemory()
                //配置client-id
                .withClient("admin")
                //配置client-secret
                .secret(passwordEncoder.encode("123123"))
                //配置访问令牌的有效时间(s)
                // .accessTokenValiditySeconds(10)
                //重定向跳转路径,用于授权成功后跳转的路径
                .redirectUris("http://localhost:8081/login")
                //配置申请权限的范围
                .scopes("all")
                //设置刷新令牌有效期
                .refreshTokenValiditySeconds(86400)
                //设置令牌有效期
                .accessTokenValiditySeconds(3600)
                //开启自动授权
                .autoApprove(true)
                //grant_type,授权类型,使用授权码模式
                // .authorizedGrantTypes("authorization_code");
                //使用密码模式
                .authorizedGrantTypes("password", "refresh_token", "authorization_code");
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        //获取密钥需要进行身份认证,单点登录必须要进行配置
        security.tokenKeyAccess("isAuthenticated()");
    }
}
